package com.chen.blog.controller;


import com.chen.blog.common.api.CommonResult;
import com.chen.blog.common.constant.MessageConstant;
import com.chen.blog.model.Type;
import com.chen.blog.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chen
 * @since 2022-01-24
 */
@Api(tags = "分类管理 TypeController")
@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @ApiOperation("新增分类")
    @PostMapping
    public CommonResult add(@RequestBody Type type) {
        return typeService.add(type);
    }

    @ApiOperation("删除分类")
    @DeleteMapping("/{ids}")
    public CommonResult delete(@PathVariable Integer[] ids) {
        return typeService.deleteByIds(ids);
    }

    @ApiOperation("更新分类")
    @PutMapping
    public CommonResult update(@RequestBody Type type) {
        return typeService.updateName(type);
    }

    @ApiOperation("检索分类")
    @GetMapping("/{currentPage}/{pageSize}")
    public CommonResult get(@PathVariable(value = "currentPage") Integer currentPage, @PathVariable(value = "pageSize") Integer pageSize, Type type) {
        return typeService.getByParamAndPage(currentPage, pageSize, type);
    }

    @ApiOperation("检索分类和其对应的博客数量")
    @GetMapping("/blog")
    public List<Map<String, Object>> findBlogCountByType() {
        return typeService.findBlogCountByType();
    }

    @ApiOperation("检索指定分类下的博客")
    @GetMapping("/{id}/blog/detail")
    public List<Map<String, Object>> findBlogByTypeId(@PathVariable Integer id) {
        return typeService.findBlogByTypeId(id);
    }
}
